var app = getApp();
var api = require('../../api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order:{},
    order_id:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options) {
      if (options.order_id) {
        this.setData({
          order_id: options.order_id
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.orderDetail()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var page = this
    var user = wx.getStorageSync('user_info');
    return {
      path: '/pages/index/index?pid=' + user.user_id,
    }
  },
  orderDetail:function(){
    var page = this;
    wx.showLoading({
      title: '加载中',
    })
    app.request({
      url: api.shop.order_detail,
      method: 'POST',
      data: {
        order_id: page.data.order_id
      },
      success: function (res) {
        if (res.errcode == 0) {
          page.setData({
            order: res.data,
          })
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  //订单支付
  doPay: function () {
    var page = this
    wx.showLoading({
      title: '加载中',
    })
    //订单获取支付
    app.request({
      url: api.order.get_pay_data,
      method: 'POST',
      data: {
        order_id: page.data.order.coreorder_id,
        pay_type: 'WECHAT_PAY'
      },
      success: function (result) {
        wx.hideLoading();
        if (result.errcode == 0) {
          var pay_data = result.data;
          wx.requestPayment({
            timeStamp: pay_data.timeStamp,
            nonceStr: pay_data.nonceStr,
            package: pay_data.package,
            signType: pay_data.signType,
            paySign: pay_data.paySign,
            success: function (pres) {
              wx.showToast({
                title: '订单支付成功',
                icon: 'success'
              });
              setTimeout(function () {
                wx.navigateTo({
                  url: '../pay-success/pay-success?order_id=' + page.data.order_id,
                })
              }, 1000)
            },
            fail: function (res) {
              wx.showToast({
                title: '订单未支付',
                image: '/images/icon-warning.png'
              });
            }
          });
        } else {
          wx.showModal({
            title: '提示',
            content: result.msg,
            showCancel: false
          });
        }
      }
    });
  },
  doReturn:function(){
    var page =this
    wx.navigateTo({
      url: '../apply-for-return/apply-for-return?order_id='+page.data.order_id+'&total_price='+page.data.order.total_fee,
    })
  },
  doShip:function(){
    var page = this
    wx.navigateTo({
      url: '../logistics/logistics?order_id=' + page.data.order_id,
    })
  },
  //点击复制
  copyText: function (e) {
    console.log(e)
    wx.setClipboardData({
      data: e.currentTarget.dataset.text,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },
})